package com.ht.demo.admin.controller.system;

import com.ht.demo.admin.annotation.LoginUser;
import com.ht.demo.common.appexception.AppException;
import com.ht.demo.common.appexception.ResultData;
import com.ht.demo.common.enums.OperateTypeEnum;
import com.ht.demo.common.page.PageBO;
import com.ht.demo.common.pojo.bo.system.SysDictDataBO;
import com.ht.demo.common.pojo.bo.system.SysDictTypeBO;
import com.ht.demo.common.pojo.bo.system.SysUserBO;
import com.ht.demo.common.pojo.qto.system.SysDictDataQTO;
import com.ht.demo.common.pojo.qto.system.SysDictTypeQTO;
import com.ht.demo.service.annotation.LoggerInfo;
import com.ht.demo.service.annotation.Permission;
import com.ht.demo.service.manager.system.SysDictManager;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author hdengxian
 * @Date 2023/5/25 8:55
 */
@RestController
@RequestMapping("${basePath}")
public class SysDictController {
    @Resource
    private SysDictManager sysDictManager;

    @Permission(permissionId = "system:dict:add", desc = "新增字典")
    @LoggerInfo(title = "字典管理", operateType = OperateTypeEnum.INSERT)
    @PostMapping("/system/dictType/save")
    public ResultData<Boolean> saveDictType(@LoginUser SysUserBO user, @RequestBody SysDictTypeBO dictTypeBO) throws AppException {
        return new ResultData<>(sysDictManager.saveDictType(user, dictTypeBO));
    }

    @Permission(permissionId = "system:dict:edit", desc = "修改字典")
    @LoggerInfo(title = "字典管理", operateType = OperateTypeEnum.UPDATE)
    @PutMapping("/system/dictType/update")
    public ResultData<Boolean> updateDictType(@LoginUser SysUserBO user, @RequestBody SysDictTypeBO dictTypeBO) throws AppException {
        return new ResultData<>(sysDictManager.updateDictType(user, dictTypeBO));
    }

    @Permission(permissionId = "system:dict:remove", desc = "删除字典")
    @LoggerInfo(title = "字典管理", operateType = OperateTypeEnum.DELETE)
    @DeleteMapping("/system/dictType/delete/{sysDictTypeId}")
    public ResultData<Boolean> deleteDictType(@PathVariable Long sysDictTypeId) throws AppException {
        return new ResultData<>(sysDictManager.deleteDictType(sysDictTypeId));
    }

    @GetMapping("/system/dict/type/{sysDictTypeId}")
    public ResultData<SysDictTypeBO> findDictTypeById(@PathVariable Long sysDictTypeId) throws AppException {
        return new ResultData<>(sysDictManager.findDictTypeById(sysDictTypeId));
    }


    @GetMapping("/system/dictType/forPage")
    public ResultData<PageBO<SysDictTypeBO>> findDictTypeForPage(SysDictTypeQTO qto) throws AppException {
        return new ResultData<>(sysDictManager.findDictTypeForPage(qto));
    }

    @GetMapping("/system/dict/type/optionselect")
    public ResultData<List<SysDictTypeBO>> findDictTypeList() throws AppException {
        SysDictTypeQTO qto = new SysDictTypeQTO();
        qto.setNeedPaging(false);
        PageBO pageBO = sysDictManager.findDictTypeForPage(qto);
        return new ResultData<>(pageBO.getData());
    }

    //===============================================================================

    @Permission(permissionId = "system:dict:add", desc = "新增字典")
    @LoggerInfo(title = "字典数据管理", operateType = OperateTypeEnum.INSERT)
    @PostMapping("/system/dictData/save")
    public ResultData<Boolean> saveDictData(@LoginUser SysUserBO user, @RequestBody SysDictDataBO dictDataBO) throws AppException {
        return new ResultData<>(sysDictManager.saveDictData(user, dictDataBO));
    }

    @Permission(permissionId = "system:dict:edit", desc = "修改字典")
    @LoggerInfo(title = "字典数据管理", operateType = OperateTypeEnum.UPDATE)
    @PutMapping("/system/dictData/update")
    public ResultData<Boolean> updateDictData(@LoginUser SysUserBO user, @RequestBody SysDictDataBO dictDataBO) throws AppException {
        return new ResultData<>(sysDictManager.updateDictData(user, dictDataBO));
    }

    @Permission(permissionId = "system:dict:remove", desc = "删除字典")
    @LoggerInfo(title = "字典数据管理", operateType = OperateTypeEnum.DELETE)
    @DeleteMapping("/system/dictData/delete/{sysDictDataId}")
    public ResultData<Boolean> deleteDictData(@PathVariable String sysDictDataId) throws AppException {
        return new ResultData<>(sysDictManager.deleteDictData(sysDictDataId));
    }

    @GetMapping("/system/dict/data/{sysDictDataId}")
    public ResultData<SysDictDataBO> findDictDataById(@PathVariable String sysDictDataId) throws AppException {
        return new ResultData<>(sysDictManager.findDictDataById(sysDictDataId));
    }

    @GetMapping("/system/dict/data/type/{dictType}")
    public ResultData<List<SysDictDataBO>> findDictDataListByType(@PathVariable String dictType) throws AppException {
        return new ResultData<>(sysDictManager.findDictDataListByType(dictType));
    }

    @GetMapping("/system/dictData/forPage")
    public ResultData<PageBO<SysDictDataBO>> findDictDataForPage(SysDictDataQTO qto) throws AppException {
        return new ResultData<>(sysDictManager.findDictDataForPage(qto));
    }

}
